package cn.ztr.ymcc.service.impl;

import cn.ztr.ymcc.domain.KillActivity;
import cn.ztr.ymcc.domain.KillCourse;
import cn.ztr.ymcc.mapper.KillActivityMapper;
import cn.ztr.ymcc.mapper.KillCourseMapper;
import cn.ztr.ymcc.result.JsonResult;
import cn.ztr.ymcc.service.IKillCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ztr
 * @since 2023-03-14
 */
@Service
public class KillCourseServiceImpl extends ServiceImpl<KillCourseMapper, KillCourse> implements IKillCourseService {
    @Autowired
    private KillActivityMapper killActivityMapper;


    public boolean save(KillCourse killCourse){

            //1 查询所属秒杀活动信息
            KillActivity killActivity = killActivityMapper.selectById(killCourse.getActivityId());
            //2 查询当前活动已经的最大课程killsort
            Long killSort = baseMapper.selectMaxKillSort(killActivity.getId());

            //3 添加其它信息
            killCourse.setStartTime(killActivity.getBeginTime())
                    .setEndTime(killActivity.getEndTime())
                    .setKillSort(Math.toIntExact(killSort));

            int insert = baseMapper.insert(killCourse);

            return insert==1?true:false;

    }

}
